package com.ruoyi.common.utils.file;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文件下载util
 *
 * @author Mr.Wei
 * @date 2020-10-15 10:31
 */
public class FileDownloadUtils {

    /**
     * 生成zip文件
     */
    public static void genZip(HttpServletRequest request, HttpServletResponse response, byte[] data, String fileName) throws IOException {
        response.reset();
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        fileName += fileName.endsWith(".zip") ? "" : ".zip";
        response.setHeader("Content-Disposition", "attachment;filename=" + FileUtils.setFileDownloadHeader(request, fileName));
        response.addHeader("Content-Length", "" + data.length);
        response.setCharacterEncoding("utf-8");
    }

}
